package by.exposit.controller;

import by.exposit.domain.Project;
import by.exposit.domain.Request;
import by.exposit.domain.Status;
import by.exposit.domain.User;
import by.exposit.service.ProjectService;
import by.exposit.service.RequestService;
import by.exposit.service.StatusService;
import by.exposit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by HaRiA on 30.07.2014.
 */
@Controller
@RequestMapping("requests")
public class RequestController {
    @Autowired
     private RequestService requestService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private StatusService statusService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "new", method = RequestMethod.GET)
    public ModelAndView newRequest(HttpSession session)
    {
        ModelAndView mav = new ModelAndView();
        User user =  (User) session.getAttribute("user");
        String page = "requests/new";
        session.setAttribute("prevpage", page);
        List<Project> projects = projectService.projectUserList(user.getUserId());
        mav.addObject("projects", projects);
        mav.setViewName(page);
        return mav;
    }

    @RequestMapping(value =  "create", method = RequestMethod.POST)
    public ModelAndView create(@RequestParam(value = "projectId",required = true) Long projectId,
                               @RequestParam(value = "moneyNeeded",required = true) Long moneyNeeded,
                               @RequestParam(value = "period", required = true) Long period, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        Project project = projectService.retrieveProject(projectId);
        Request request = requestService.getRequestByProject(project);

        if(request == null){
        Status status =  statusService.retrieveStatus(Long.valueOf(0));
        requestService.saveRequest(new Request(project,moneyNeeded,status,period));
        String home = (String) session.getAttribute("homepage");
        mav.setViewName(home);
        }
        else
        {
            mav.addObject("error","You can't make more than one request for one project!");
            mav.setViewName("not_found_page");
        }
        return mav;
    }

    @RequestMapping(value="themedRequests",method=RequestMethod.GET)
    public ModelAndView getListOfThemedRequests(
            //@RequestParam(value = "user")

            HttpSession session
    )  //выбирает все запросы  с темами, которые имеются в проектах пользователя
    {
        ModelAndView mav = new ModelAndView();

       User user = (User) session.getAttribute("user");
       String prev_page =(String) session.getAttribute("prevpage");
       mav.addObject(prev_page);

        List<Request> requests =  requestService.requestFieldList(user) ;
        if(!requests.isEmpty()) {
            mav.addObject("requests", requests);
        }
        mav.setViewName("../requests/list");
        mav.addObject("prev_page",prev_page);
        return mav;
    }

    @RequestMapping(value="usersRequests/{userId}",method=RequestMethod.GET)
    public ModelAndView getListOfUsersRequests(
            @PathVariable    String userId,
            HttpSession session
    )
    {ModelAndView mav = new ModelAndView();
        String prev_page =(String) session.getAttribute("prevpage");
       // User user= (User) session.getAttribute("user");

         List<Request> requests = requestService.requestUserList(Long.valueOf(userId));
        if(!requests.isEmpty()) {
            mav.addObject("requests", requests);
        }
        mav.addObject("prev_page",prev_page);
         mav.setViewName("requests/list");
        return mav;
    }

    @RequestMapping(value = "request/{requestId}",method=RequestMethod.GET)
    public ModelAndView projectInfo(@PathVariable String requestId,HttpSession session)
    {
        ModelAndView mav = new ModelAndView();
        Request request = requestService.retrieveRequest(Long.valueOf(requestId));
        mav.addObject("request",request);
        mav.addObject("current_user",(User)session.getAttribute("user"));
        mav.setViewName("requests/request");
        session.setAttribute("prevpage","redirect:/requests/list/"+request.getProject().getUser().getUserId());
        return mav;

    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public ModelAndView delete( @RequestParam(value = "requestId",required = true) String requestId,
                                HttpSession session)
    {

        ModelAndView mav = new ModelAndView();
        Request request = requestService.retrieveRequest(Long.valueOf(requestId));
        String home_page =(String)session.getAttribute("homepage");
        User user =(User)session.getAttribute("user");
        if(user.getUserId() == request.getProject().getUser().getUserId()) {
        requestService.remove(Long.valueOf(requestId));
        mav.setViewName("../requests/list/"+user.getUserId());
        }
        else
        {    mav.addObject("error","You can't delete this project!");
            mav.setViewName("not_found_page");
        }
        return mav;
    }

}
